package com.example.meituan;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.meituan.activity.man.ManageManActivity;
import com.example.meituan.activity.man.RegisterManActivity;
import com.example.meituan.activity.user.RegisterUserActivity;
import com.example.meituan.activity.user.UserMainActivity;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.db.DBUntil;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        Context context = this;
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        DBUntil dbUntil=new DBUntil(this);
        DBUntil.con=dbUntil.getWritableDatabase();

        RadioButton yhRedio=findViewById(R.id.login_yh);
        yhRedio.setChecked(true);//让运行时候用户按钮自动选择

        TextView zcyh=findViewById(R.id.login_dlyh);
        zcyh.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //跳转到注册用户界面
                                        Intent intent=new Intent(MainActivity.this, RegisterUserActivity.class);
                                        startActivity(intent);

                                    }
                                }
        );
        TextView zcsj=findViewById(R.id.login_dlsj);
        zcsj.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //跳转到注册用户界面
                                        Intent intent=new Intent(MainActivity.this, RegisterManActivity.class);
                                        startActivity(intent);

                                    }
                                }
        );

        EditText accountText=findViewById(R.id.login_account);
        EditText pwdText=findViewById(R.id.login_pwd);
        RadioButton radio=findViewById(R.id.login_yh);
        TextView denglu=findViewById(R.id.login_dl);
        denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account=accountText.getText().toString();
                String pwd=pwdText.getText().toString();
                if(account.isEmpty()){
                    Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
                }
                else if(pwd.isEmpty()){
                    Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    if(radio.isChecked()) {
                        //用户
                        int a = AdminDao.loginUser(account, pwd);
                        if (a == 1) {
                            SharedPreferences sharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("user_id",account); // 用户ID
                            editor.putBoolean("is_logged_in", true); // 登录状态
                            editor.apply();
                            Toast.makeText(MainActivity.this, "用户登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(MainActivity.this, UserMainActivity.class);
                            startActivity(intent);
                        }
                        else{
                            Toast.makeText(MainActivity.this, "用户登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                    else{
                        //商家
                        int a = AdminDao.loginBusiness(account, pwd);
                        if (a == 1) {
                            SharedPreferences sharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("user_id",account); // 用户ID
                            editor.putBoolean("is_logged_in", true); // 登录状态
                            editor.apply();
                            Toast.makeText(MainActivity.this, "商家登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(MainActivity.this, ManageManActivity.class);
                            startActivity(intent);
                        }
                        else{
                            Toast.makeText(MainActivity.this, "商家登录失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }

            }
        });
    }
}